{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 插入行或列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl import Workbook\n",
    "\n",
    "wb = Workbook()\n",
    "ws = wb.active\n",
    "\n",
    "data = [[61,16,39,78],\n",
    "        [69,67,15,13],\n",
    "        [48,78,99,90],\n",
    "        [69,43,46,87]\n",
    "      ]\n",
    "for row in data:\n",
    "    ws.append(row)\n",
    "\n",
    "ws1 = wb.copy_worksheet(ws)\n",
    "\n",
    "#在第3行插入一行\n",
    "ws1.insert_rows(3,2)\n",
    "\n",
    "#在第2列插入3列\n",
    "ws1.insert_cols(2)\n",
    "\n",
    "wb.save(r'C:\\Users\\zhangjunhong\\Desktop\\insert_rowcol.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 删除行或列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl import Workbook\n",
    "\n",
    "wb = Workbook()\n",
    "ws = wb.active\n",
    "\n",
    "data = [[61,16,39,78],\n",
    "        [69,67,15,13],\n",
    "        [48,78,99,90],\n",
    "        [69,43,46,87]\n",
    "      ]\n",
    "for r in data:\n",
    "    ws.append(r)\n",
    "\n",
    "#删除第三行\n",
    "ws.delete_rows(3) \n",
    "\n",
    "#从第2列开始删除2列\n",
    "ws.delete_cols(2,2)\n",
    "\n",
    "wb.save(r'C:\\Users\\zhangjunhong\\Desktop\\delete_rowcol.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 行高/列宽设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl.workbook import Workbook\n",
    "\n",
    "wb = Workbook()\n",
    "ws = wb.active\n",
    "\n",
    "#调整列宽\n",
    "ws.column_dimensions['A'].width = 20.0\n",
    " \n",
    "#调整行高\n",
    "ws.row_dimensions[1].height = 40\n",
    "\n",
    "wb.save(r'C:\\Users\\zhangjunhong\\Desktop\\row_col.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 隐藏行/列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl import load_workbook\n",
    "\n",
    "wb = Workbook()\n",
    "ws = wb.active\n",
    "\n",
    "ws.row_dimensions.group(7, 10, hidden=True)    # 隐藏7到10行\n",
    "\n",
    "ws.column_dimensions.group('D', 'F', hidden=True)    # 隐藏D到F列\n",
    "\n",
    "wb.save(r'C:\\Users\\zhangjunhong\\Desktop\\hidden.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 批量设置多行/多列的行高/列宽"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl.workbook import Workbook\n",
    "\n",
    "wb = Workbook()\n",
    "ws = wb.active\n",
    "\n",
    "#批量调整列宽\n",
    "for col in ['A','B','C']:\n",
    "    ws.column_dimensions[col].width = 20.0\n",
    " \n",
    "#批量调整行高\n",
    "for row in [1,2,3]:\n",
    "    ws.row_dimensions[row].height = 40\n",
    "\n",
    "wb.save(r'C:\\Users\\zhangjunhong\\Desktop\\all_row_col.xlsx')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "789px",
    "left": "69px",
    "top": "110px",
    "width": "308.507px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
